package me.caofeng.controller;

import com.github.pagehelper.PageInfo;
import me.caofeng.annotation.Authentication;
import me.caofeng.enums.OrderStatus;
import me.caofeng.enums.ResponseCode;
import me.caofeng.enums.Role;
import me.caofeng.pojo.Order;
import me.caofeng.pojo.Page;
import me.caofeng.pojo.Response;
import me.caofeng.pojo.User;
import me.caofeng.service.api.IOrderService;
import me.caofeng.util.Constant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.List;

/**
 * Copyright © 2017曹峰. All rights reserved.
 *
 * @Prject: mall
 * @Package: me.caofeng.controller
 * @Description:
 * @Author: 曹峰 blog.caofeng.me
 * @Date: 2017-07-01 19:47
 * @Version: V1.0
 */
@Controller
@RequestMapping(value = "order")
@Authentication
public class OrderController {
    private Logger LOGGER = LogManager.getLogger(OrderController.class);

    @Autowired
    private IOrderService orderService;

    @RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Response<Order> getOrderDetail(@PathVariable("id") String id, HttpSession session) {
        User user = (User) session.getAttribute(Constant.USER);
        return orderService.getOrderDetail(user.getUid(), id);
    }

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Response<PageInfo<List<Order>>> getOrdersAsPageInfo(
            @RequestParam(value = "status", defaultValue = "-1") Integer status,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            HttpSession session) {
        User user = (User) session.getAttribute(Constant.USER);
        return orderService.getOrdersAsPageInfo(user.getUid(), OrderStatus.getStatus(status), Role.MEMBER, page, size);
    }

    @RequestMapping(value = "{page}", method = RequestMethod.GET)
    @ResponseBody
    public Response<Page<List<Order>>> getOrders(
            @PathVariable("page") Integer page,
            @RequestParam(value = "status", defaultValue = "-1") Integer status,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            HttpSession session) {
        User user = (User) session.getAttribute(Constant.USER);
        return orderService.getOrders(user.getUid(), OrderStatus.getStatus(status), Role.MEMBER, page, size);
    }


    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Response<Order> buildOrderFromCart(@RequestParam("addressId") Integer addressId, HttpSession session) throws SQLException {
        User user = (User) session.getAttribute("user");
        return orderService.buildOrderFromCart(user.getUid(), addressId);
    }

    @RequestMapping(value = "product/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Response<Order> addOrder(@PathVariable("id") Integer id,
                             @RequestParam("count") Integer count,
                             @RequestParam("addressId") Integer addressId,
                             HttpSession session) {
        User user = (User) session.getAttribute("user");
        return orderService.buildOrderFromProduct(user.getUid(), addressId, id, count);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    @ResponseBody
    public Response cancelOrder(@PathVariable("id") String orderId,HttpSession session) {
        User user = (User) session.getAttribute(Constant.USER);
        return orderService.cancelOrder(user.getUid(),orderId);
    }

    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public Response deleteOrder(@PathVariable("id") String orderId,HttpSession session){
        User user = (User) session.getAttribute(Constant.USER);
        return orderService.deleteOrder(user.getUid(),orderId);
    }

    @RequestMapping(value = "{id}" ,method = RequestMethod.POST)
    @ResponseBody
    public Response signOrder(@PathVariable("id") String orderId,HttpSession session){
        User user = (User) session.getAttribute(Constant.USER);
        return orderService.signOrder(orderId,user.getUid());
    }
}
